package com.example.demo.controller;

import com.example.demo.constants.Constants;
import com.example.demo.enums.BookStatusEnum;
import com.example.demo.modal.BookInfo;
import com.example.demo.modal.PageRequest;
import com.example.demo.modal.PageResponse;
import com.example.demo.modal.Result;
import com.example.demo.service.BookService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
@Slf4j
@Component
@RequestMapping("/book")
@RestController
public class BookController {
    @Autowired
    private BookService bookService;



    @RequestMapping("/addBook")
    public String addBook(BookInfo bookInfo){
        log.info("添加图书",bookInfo);
        if(bookInfo == null){
            return "没有收到图书信息";
        }
        if (!StringUtils.hasLength(bookInfo.getBookName())
            || !StringUtils.hasLength(bookInfo.getAuthor())
            || bookInfo.getCount() == null
            || bookInfo.getPrice() == null
            || !StringUtils.hasLength(bookInfo.getPublish())
            || bookInfo.getStatus() == null) {
            return "信息不完整";
        }
        try {
            bookService.addBook(bookInfo);
            return "";
        }catch (Exception e){
            log.error("添加图书失败",e);
        }
        return "信息插入失败, 联系后台管理员";
    }

    @RequestMapping("getListByPage")
    public Result<PageResponse<BookInfo>> getListByPage(PageRequest pageRequest, HttpServletRequest request){
        log.info("获取图书列表{}",pageRequest);
        HttpSession s = request.getSession(false);
        if (session.getAttribute(Constants.SESSION_USER_KEY) == null ||
            !StringUtils.hasLength((String) session.getAttribute(Constants.SESSION_USER_KEY))){
            return Result.unLogin();
        }
        PageResponse<BookInfo> bookInfoPageResponse = new PageResponse<>();
        try {
            bookInfoPageResponse = bookService.getListByPage(pageRequest);
        }catch (Exception e){
            log.error("获取图书列表失败");
            return Result.fail();
        }
        return Result.success(bookInfoPageResponse);
    }

    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId){
        log.info("获取图书信息, bookId:",bookId);
        //参数校验

        return bookService.queryBookById(bookId);
    }

    @RequestMapping("/updateBook")
    public String updateBook(BookInfo bookInfo){
        log.info("更新图书,bookInfo{}",bookInfo);
        try{
            Integer result = bookService.upadateBook(bookInfo);
            if (result > 0){
                return "";
            }
            return "图书更新失败";
        }catch (Exception e){
            log.error("更新图书失败e: {}",e);
            return "数据操作失败";
        }
    }

    @RequestMapping("/deleteBook")
    public String deleteBook(Integer bookId){
        log.info("删除图书,bookId{}",bookId);
        try{
            BookInfo bookInfo = new BookInfo();
            bookInfo.setId(bookId);
            bookInfo.setStatus(BookStatusEnum.DELETED.getCode());
            Integer result = bookService.upadateBook(bookInfo);
            if (result > 0){
                return "";
            }
            return "图书删除失败";
        }catch (Exception e){
            log.error("更新图书失败e: {}",e);
            return "数据操作失败";
        }
    }

    @RequestMapping("/batchDeleteBook")
    public boolean batchDeleteBook(@RequestParam List<Integer> ids){
        log.info("批量删除图书,ids{}",ids);
        try{
            bookService.batchDeleteBook(ids);
            return true;
        }catch (Exception e){
            log.error("批量删除图书失败e: {}",e);
            return false;
        }
    }
}
